package com.yueyang.sojourn.server.service.sojourn.order;

import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.model.enums.OrderEvaluateStatus;
import com.yueyang.sojourn.server.model.enums.OrderStatusEnum;
import com.yueyang.sojourn.server.model.enums.OrderTypeEnum;
import com.yueyang.sojourn.server.model.po.MemberMerch;
import com.yueyang.sojourn.server.model.po.SojournOrder;
import com.yueyang.sojourn.server.service.member.MemberMerchService;
import com.yueyang.sojourn.server.service.member.MemberMerchSnapService;
import com.yueyang.sojourn.server.service.sojourn.sequence.SojournSequnceManager;
import com.yueyang.uc.model.UcUserV1;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 会员订单service
 * @author Stephen.S
 * @since 2020/03/27
 */
@Service
public class MemberMerchOrderService {
    @Autowired
    private SojournOrderService orderService;
    @Autowired
    private MemberMerchService memberMerchService;
    @Autowired
    private MemberMerchSnapService memberMerchSnapService;
    @Autowired
    private SojournSequnceManager sojournSequnceManager;


    /**
     * 创建会员订单
     * @param loginUser
     * @param memberMerchId
     */
    public SojournOrder createMemberOrder(UcUserV1 loginUser, Integer memberMerchId) {
        MemberMerch memberMerch = this.memberMerchService.findById(memberMerchId);
        JAssert.pass(memberMerch != null, "系统异常，会员商品不存在,id=" + memberMerchId);
        SojournOrder order = new SojournOrder();
        order.setCreator(loginUser.getId());
        order.setCreatorName(loginUser.getNickName());
        order.setCreatorPhone(loginUser.getPhone());
        order.setContactPhone(loginUser.getPhone());
        order.setContactName(loginUser.getNickName());
        order.setCreatedTime(new Date());
        order.setDiscounted(memberMerch.getPrice().subtract(memberMerch.getRealPrice()));
        order.setDiscountOnePrice(memberMerch.getRealPrice());
        order.setEvaluateStatus(OrderEvaluateStatus.待评价.getValue());
        order.setMerchCount(1);
        order.setMerchEffectDate(new Date());
        order.setMerchId(memberMerchId.longValue());
        order.setMerchSale(null);
        order.setMerchTitle(memberMerch.getName());
        order.setMerchPicAttachId(-1L);
        order.setMerchPrice(memberMerch.getPrice());
        order.setMerchProjectId(-1L);
        order.setPayEndTime(DateUtils.addMinutes(new Date(), 30));
        order.setSn(sojournSequnceManager.nextValueOfOrder(OrderTypeEnum.VIP));
        order.setStatus(OrderStatusEnum.TO_BE_PAY.getCode());
        order.setTotalPrice(memberMerch.getRealPrice());
        order.setType(OrderTypeEnum.VIP.getCode());
        order.setUpdatedTime(new Date());
        order.setUpdator(loginUser.getId());
        this.orderService.add(order, null);
        return order;
    }

}
